package com.service.impl;

import com.service.ISmsRedisService;
import com.util.JsonResult;
import com.util.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SmsRedisServiceImpl implements ISmsRedisService {
    @Autowired
    private StringRedisTemplate template;

    @Override
    public void setVerifyCode(String phone, String code) {
        //key 必须唯一、可读、有效、灵活
        String key = RedisKeys.VERIFY_CODE.join(phone);
        template.opsForValue()
                //参数1：key值；  参数2：value值；  参数3：有效时间；  参数4：时间单位
                .set(key, code, RedisKeys.VERIFY_CODE.getTime(), TimeUnit.SECONDS);
    }

    @Override
    public String getVerifyCode(String phone) {
        String key = RedisKeys.VERIFY_CODE.join(phone);
        return template.opsForValue().get(key);
    }
}
